SpringBoot 整合 Swagger
Java Doc
常用 的标签
参考资料 Swagger使用和注释介绍
下面记录常用的标签 类:
@author@version@param形参名 对参数的描述@deprecated表示这个类已经过时
方法
@return@param形参名 对参数的描述@throws或@exception@deprecated表示这个方法已经过时
例子:
/**
* <b>这是对这个方法的描述</b><br/>
* 这是对这个方法的描述<br/>
* 这是对这个方法的描述<br/>
* 这是对这个方法的描述<br/>
* 这是对这个方法的描述<br/>
*
* @param a 输入一个StringBuffer用来测试
* @param b 输入一个String用来测试2
* @return String
* @throws Exception 返回一个空值错误
* @author alsritter
*/
public String test(StringBuffer a, String b) throws Exception {
if (a == null) {
throw new Exception();
}
return "hello";
}
注意:描述方法的直接放在开头就可以了
<br/>
<p>表示一整段文字的描述,没有换行</p>
<b>表示文字用粗黑体</b>
{@code 这里是代码}表示在注释插入代码,例如List<String>里面的<>就不会被识别为一个 HTML 标签
@link 和 @see 的使用
写代码的时候可能要写一些注释,把内容相互关联起来,以便直接定位到某个代码类或者啥的。
@see 标签允许用户引用其他类的文档。具体使用方式如下:
/**
* 注解必须在行首,且每个 @see 会用逗号隔开
*
* @see classname
* @see fully-qualified-classname
* @see fully-qualified-classname#方法名称(不用加括号)
* @see fully-qualified-classname#属性名称
*
* 例如
* @see com.alsritter.entity.MyTestClass#test
*/
@link 标签作用是链接到另一个地方,与 @see 用途基本相同,但是使用时不需要在行首,且要用 { } 包围
/**
* {@link com.alsritter.entity.MyTestClass#test}
* 如下
*/
/**
* Returns a stream of code point values from this sequence. Any surrogate
* pairs encountered in the sequence are combined as if by {@linkplain
* Character#toCodePoint Character.toCodePoint} and the result is passed
* to the stream. Any other code units, including ordinary BMP characters,
* unpaired surrogates, and undefined code units, are zero-extended to
* {@code int} values which are then passed to the stream.
*
* <p>If the sequence is mutated while the stream is being read, the result
* is undefined.
*
* @return an IntStream of Unicode code points from this sequence
* @since 1.8
*/
编码问题
用 IDEA 生成 Java Doc 时,在 Tools --> Gerenate JavaDoc 面版的 Other command line arguments 栏里输入:-encoding utf-8 -charset utf-8
Swagger
参考资料 官方文档 参考资料 Spring Boot 2.x基础教程:使用SpringFox 3生成Swagger文档
Swagger2 是一款开源的文档生成器,它可以构建一份 RESTful API 文档来描述所有的接口信息
生产环境中,要关闭 swagger
springfox:
documentation:
enabled: false
当我们在使用 Spring MVC 写接口的时候,为了生成 API文档,为了方便整合 Swagger,都是用这个 SpringFox的这套封装
配置环境
果然不应该第一时间就去看各种教程而是应该直接翻官网,官方实际已经删除 springfox-swagger2 springfox-swagger-ui 使用方法和以前也不同,例如已经删除了 @EnableSwagger2
<!-- 使用官方的远程库,好像要挂梯子 -->
<repositories>
<repository>
<id>jcenter-snapshots</id>
<name>jcenter</name>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>
<!-- 使用全新的依赖 -->
<!-- 只需导入这个 starter -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
然后只需在启动类上加上一个 @EnableOpenApi 注解
@SpringBootApplication
@EnableOpenApi
public class StudyswaggerApplication {
public static void main(String[] args) {
SpringApplication.run(StudyswaggerApplication.class, args);
}
}
然后就可以直接访问
http://localhost:8080/swagger-ui/
注意:如果用到了 Spring Security 别忘了加上白名单
String[] SWAGGER_WHITELIST = {
"/swagger-ui.html",
"/swagger-ui/*",
"/swagger-resources/**",
"/v2/api-docs",
"/v3/api-docs",
"/webjars/**"
};
httpSecurity.cors()
.antMatchers(SWAGGER_WHITELIST).permitAll()
常用的注解
更多注解的配置细节参考 swagger2 注解说明
@Api:用在 controller 类,描述 API 接口@ApiOperation:描述接口方法@ApiModel